package com.aliang.book_java1.controller;

import com.aliang.book_java1.service.LogicalManage;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/otherQuestion")
@Api(value = "示例控制器2", tags = "业务接口（其他面试问题）")
public class OtherQuestion {

    @Resource
    LogicalManage logicalManage;

    /**
     *创建一个excel文件上传接口，解析文件数据并返回。
     * @param file
     * @param response
     * @param request
     */
    @RequestMapping(value = "/uploadExcel", method = { RequestMethod.POST })
    //@ResponseBody
    @ApiOperation(value = "上传Excel", notes = "上传Excel")
    public Map<String,Object> uploadExcel(
            @RequestParam(value = "file", required = false) MultipartFile file,
            HttpServletResponse response, HttpServletRequest request) {

        //这里可以封装一个返回值类，由于时间紧急，我这边做简单处理
        Map<String,Object> resultMap=new HashMap<>();
        try {
            String type = file.getOriginalFilename();
            //System.out.println("--------type--------"+ type);
            JSONArray jsonArray=logicalManage.uploadExcel(type,file);
            resultMap.put("code",200);
            resultMap.put("message","导入成功");
            resultMap.put("data",jsonArray);
            System.out.println("--resultMap-----"+JSON.toJSONString(resultMap));
        }catch (Exception e){
            e.printStackTrace();
            resultMap.put("code",500);
            resultMap.put("message","数据表导入失败，请联系管理员");
        }

        return resultMap;
    }


    /**
     * 返回当前电脑cpu，内存，磁盘，网卡，ip地址等数据
     * @param request
     * @param response
     */
    @RequestMapping(value = "/getComputerInfo", method = { RequestMethod.GET })
    @ApiOperation(value = "获取信息", notes = "返回当前电脑cpu，内存，磁盘，网卡，ip地址等数据")
    public Map<String,Object> getComputerInfo(HttpServletRequest request,HttpServletResponse response) {
        Map<String,Object> resultMap=new HashMap<>();
        try {
            Map<String,Object> computerInfo=logicalManage.getComputerInfo();
            resultMap.put("code",200);
            resultMap.put("message","获取成功");
            resultMap.put("data",computerInfo);
        }catch (Exception e){
            e.printStackTrace();
            resultMap.put("code",500);
            resultMap.put("message","获取，请联系管理员");
        }

        return resultMap;
    }


    @RequestMapping(value = "/checkNews", method = { RequestMethod.POST })
    @ApiOperation(value = "获取信息", notes = "传入一段新闻，字符串格式，进行分词和词频统计，并返回结构")
    public Map<String,Object> checkNews(@RequestBody Map<String, Object> map, HttpServletRequest request, HttpServletResponse response) {
        Map<String,Object> resultMap=new HashMap<>();
        try {
            //String news = "这是一段新闻内容 新闻中包含了多个词汇 这些词汇有的会重复出现 用于测试分词和词频统计";

            System.out.println("-------------"+ JSON.toJSONString(map));
            Map<String, Integer> frequencyMap = logicalManage.countWordFrequency(String.valueOf(map.get("news")));

            // 打印词频统计结果
            for (Map.Entry<String, Integer> entry : frequencyMap.entrySet()) {
                System.out.println("Word: " + entry.getKey() + ", Frequency: " + entry.getValue());
            }
            resultMap.put("code",200);
            resultMap.put("message","词频统计返回结果");
            resultMap.put("data",frequencyMap);
        }catch (Exception e){
            e.printStackTrace();
            resultMap.put("code",500);
            resultMap.put("message","词频解析失敗，请联系管理员");
        }

        return resultMap;

    }


}
